/*
 * AddUserCompetenceMenu.java
 *
 * Created on __DATE__, __TIME__
 */

package serverPresentation;

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import connection.DatabaseConnection;

import server.ServerData;

/**
 *
 * @author  __USER__
 */
public class AddUserCompetenceMenu extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	/** Creates new form AddUserCompetenceMenu */
	public AddUserCompetenceMenu() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		initComponents();

		Image im = Toolkit.getDefaultToolkit().getImage(
				"images/security-icon.gif");
		this.setIconImage(im);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		_firstNameBox = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		_lastNameBox = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		_competenceBox = new javax.swing.JTextField();
		_cancelButton = new javax.swing.JButton();
		_nextButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Adicionar Compet\u00eancia a Pessoa");

		jLabel1.setText("Primeiro Nome");
		jLabel1.setToolTipText("Primeiro Nome");

		jLabel2.setText("Apelido (Apenas \u00daltimo)");
		jLabel2.setToolTipText("Apelido (Apenas \u00daltimo)");

		jLabel3.setText("Compet\u00eancia");
		jLabel3.setToolTipText("Compet\u00eancia");

		_cancelButton.setText("Cancelar");
		_cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				cancelButtonMouseClicked(evt);
			}
		});

		_nextButton.setText("Continuar >");
		_nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				nextButtonMouseClicked(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		jLabel1))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(12,
																										12,
																										12)
																								.addComponent(
																										jLabel2)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														_firstNameBox,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														148,
																														Short.MAX_VALUE)
																												.addComponent(
																														_lastNameBox,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														148,
																														Short.MAX_VALUE)
																												.addComponent(
																														_competenceBox,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														148,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addContainerGap()
																								.addComponent(
																										_nextButton)))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		_cancelButton))
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		jLabel3)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														_firstNameBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														_lastNameBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														_competenceBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										26, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(_cancelButton)
												.addComponent(_nextButton))
								.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {
		String firstName = _firstNameBox.getText();
		String lastName = _lastNameBox.getText();
		String competence = _competenceBox.getText();
		
		if(firstName.isEmpty()||lastName.isEmpty()||competence.isEmpty()){
			JOptionPane.showMessageDialog(this, "Todos os campos têm de ser preenchidos!!");
			return;
		}
		
		DatabaseConnection dbConn = ServerData.getServerData().getDatabaseConnection();
		
		int result = dbConn.addUserCompetency(firstName, lastName, competence);
		
		if(result != 1){
			JOptionPane.showMessageDialog(this, "Não foi possível adicionar a competência. Provavelmente o nome não corresponde a um utilizador registado.");
			return;
		}else{
			JOptionPane.showMessageDialog(this, "Competência adicionada com sucesso!!");
			this.setVisible(false);
			ServerData.getServerData().getMainMenu().setVisible(true);
			this.dispose();
		}
	}

	private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
		this.setVisible(false);
		ServerData.getServerData().getMainMenu().setVisible(true);
		this.dispose();
		return;
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new AddUserCompetenceMenu().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton _cancelButton;
	private javax.swing.JTextField _competenceBox;
	private javax.swing.JTextField _firstNameBox;
	private javax.swing.JTextField _lastNameBox;
	private javax.swing.JButton _nextButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	// End of variables declaration//GEN-END:variables

}